package com.hdoit.apollo.web.shop.apis;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hdoit.apollo.logical.cargo.model.Picture;
import com.hdoit.apollo.logical.cargo.service.IPictureService;
import com.hdoit.apollo.logical.shop.model.Shop;
import com.hdoit.apollo.logical.shop.service.IShopService;
import com.tennetcn.authority.component.webapi.BaseApi;
import com.tennetcn.authority.component.webapi.ResponseModel;
import com.tennetcn.authority.component.webapi.ResponseStatus;
import com.tennetcn.core.common.enums.pub.YesOrNoInteger;
import com.tennetcn.core.common.utils.CommonUtils;
import com.tennetcn.core.common.utils.DateUtils;
import com.tennetcn.data.enums.ModelStatus;

@Controller
@RequestMapping(value = "/api/v1/shop/",method = { RequestMethod.GET,RequestMethod.POST},produces = "application/json;charset=UTF-8")
@CrossOrigin(origins = "*", maxAge = 3600)
@Api(value="/api/v1/shop")
public class ShopApi extends BaseApi{
	
	@Autowired
	private IShopService shopService;
	
	@Autowired
	private IPictureService pictureService;
	
	private final String queryShopInfoApi="queryShopInfo";
	
	private final String saveShopInfoApi="saveShopInfo";

	@RequestMapping(value = queryShopInfoApi)
    @ApiOperation(notes = queryShopInfoApi, httpMethod = "POST", value = "店铺信息")
    public @ResponseBody String queryShopInfo(String shopId){
    	ResponseModel rm=new ResponseModel();
    	 Shop shopInfo=shopService.queryModel(shopId);
    	 System.err.println(shopInfo.getShopName()+"///"+shopInfo.getLogoId());
    	 rm.put("shopInfo",shopInfo);
    	return rm.toJson();
    }
	
	@RequestMapping(value = saveShopInfoApi)
    @ApiOperation(notes = saveShopInfoApi, httpMethod = "POST", value = "店铺信息")
    public @ResponseBody String saveShopInfo(Shop shop){
		Shop shoped=shopService.queryModel(shop.getShopId());
		Picture picture=new Picture();
    	if (shoped==null) {
    		shop.setCreateDate(DateUtils.getCurrent());
    		shop.setCreateUserId(getLoginId());
    		shop.setCreateUserName(getLoginName());
    		shop.setDeleteMark(YesOrNoInteger.no);
    		shop.setModelStatus(ModelStatus.add);
		}else {
			shop.setModifyDate(DateUtils.getCurrent());
			shop.setModifyUserId(getLoginId());
			shop.setModifyUserName(getLoginName());
			shop.setModelStatus(ModelStatus.update);
			shop.setDeleteMark(shoped.getDeleteMark());
			
			if (!StringUtils.isEmpty(shop.getLogoId())&&shop.getLogoId()!="") {
				
				picture.setPictureId(CommonUtils.getKeyId());
				picture.setFileId(shop.getLogoId());
				picture.setMark("logo");
				picture.setDeleteMark(YesOrNoInteger.no);
	    		picture.setModelStatus(ModelStatus.add);
	    		picture.setCreateDate(DateUtils.getCurrent());
	    		picture.setCreateUserId(getLoginId());
	    		picture.setCreateUserName(getLoginName());
			}
			
		}
    	
     	ResponseModel rm=new ResponseModel();
     	if (!StringUtils.isEmpty(picture.getPictureId())) {
     		if (!pictureService.applyChange(picture)) {
         		rm.setStatus(ResponseStatus.dataError);
    		}
		}
 
     	if(!shopService.applyChange(shop)){
     		rm.setStatus(ResponseStatus.dataError);
     	}
     	
     	return rm.toJson();
    }
}
